﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class GameMain : GlobalMembersGame_object
{
    Button a;
    static int Demo_Battle;
    public static int Id=1;
    Event @event;
    public float CurrentKeyCodeTime;
    // Start is called before the first frame update
    void Start()
    {
        int role_id=1;

        GlobalMembersGame_config. GameConfig_Init();
        
        GlobalMembersGame_resource.GameGraphRes_Init();

        GlobalMembersGame_object.GameObject_Register();

        GlobalMembersGame_attack.Game_InitAttackLibrary();
        GlobalMembersGame_control.Game_InitStateActionMap();
        GlobalMembersGame_skill.SkillLibrary_Init();

        Demo_Battle= Game_InitFight(role_id);
        Game_DemoThread((System.IntPtr)Demo_Battle);
        Application.targetFrameRate = 30;
    }

    // Update is called once per frame
    void Update()
    {
        DemoGameBattle_Loop(Demo_Battle);
       
        GlobalMembersGame_battle.GameBattle_ProcGameObject(Demo_Battle);

        if (KeyCodeTime > 0)
        {
            KeyCodeTime -= Time.deltaTime * 500;
            CurrentKeyCodeTime = KeyCodeTime;
        }
        
    }

    private void OnGUI()
    {
        @event = Event.current;
        if (Input.anyKeyDown)
        {
            if (@event.isKey)
            {
                //print(@event.keyCode);
                GlobalMembersGame_control.GameKeyboardProcKeyDown((int)Event.current.keyCode);
            }
        }
    }

    internal static void Game_DemoThread(System.IntPtr arg)
    {
        int battle_id;
        LCUI_Size_ scene_size = new LCUI_Size_();
        LCUI_Widget_ demo_scene;

        scene_size.w = 800;
        scene_size.h = 600;
        battle_id = (int)arg;
        // 设置对战场景的地面参数 
        GlobalMembersGame_battle. GameBattle_SetSceneLand(battle_id, My_Widget. Pos(50, 400), My_Widget.Size(700, 200));
        // 设置对战场景的大小 
        GlobalMembersGame_battle.GameBattle_SetSceneSize(battle_id, scene_size);
        // 获取对战场景 
        demo_scene = GlobalMembersGame_battle.GameBattle_GetScene(battle_id);
        // 设置对战场景的z-index值 
        My_Widget.Widget_SetZIndex(demo_scene, -10);
        // 设置对战场景不可被点击 
        My_Widget.Widget_SetClickableAlpha(demo_scene, 0, 0);
        // 将演示对战的对战场景加入至主界面 
        My_Widget.Widget_Container_Add(GlobalMembersMain_menu.Game_GetMainUI(), demo_scene);
        // 设置两个对战角色，由AI控制 
        GlobalMembersGame_battle.GameBattle_SetPlayer(battle_id, 1, (int)RoleID.ROLE_KUNIO, DefineConstantsMain.TRUE);
        GlobalMembersGame_battle.GameBattle_SetPlayer(battle_id, 2, (int)RoleID.ROLE_RIKI, DefineConstantsMain.FALSE);
        // 启用这两个对战角色 
        GlobalMembersGame_battle.GameBattle_EnablePlayer(battle_id, 1, DefineConstantsMain.TRUE);
        GlobalMembersGame_battle.GameBattle_EnablePlayer(battle_id, 2, DefineConstantsMain.TRUE);
        // 设置这两个对战角色为无敌 
        GameBattlePlayer_SetInvincible(battle_id, 1, DefineConstantsMain.TRUE);
        GameBattlePlayer_SetInvincible(battle_id, 2, DefineConstantsMain.TRUE);
        // 放置对战角色 
        GameDemoScene_PutPlayer(battle_id);
        // 开始游戏对战 
        GameBattle_Start(battle_id);
        // 显示对战场景 
        Widget_Show(demo_scene);
        // 进入游戏对战处理循环 
        //GameBattle_Loop(battle_id);
        // 设置按键
        ControlKey_ ctrlkey;
        GlobalMembersGame_config.GameConfig_GetKeyControl(out ctrlkey);
        /* 设置1号玩家的控制键 */
        GameBattle_SetPlayerControlKey(battle_id, 1, ctrlkey);
    }

    internal static void GameDemoScene_PutPlayer(int battle_id)
    {
        int x;
        int y;
        LCUI_Pos_ start_pos = new LCUI_Pos_();
        LCUI_Size_ scene_size = new LCUI_Size_();
        GamePlayer_[] player = new GamePlayer_[2];

        player[0] = GameBattle_GetPlayer(battle_id, 1);
        player[1] = GameBattle_GetPlayer(battle_id, 2);
        //GameObject_SetFillColor( player[0]->object, TRUE, RGB(0,0,0) );
        //GameObject_SetFillColor( player[1]->object, TRUE, RGB(0,0,0) );
        GameObject_SetShadowVisible( player[0].object_, DefineConstantsMain.TRUE);
        GameObject_SetShadowVisible( player[1].object_, DefineConstantsMain.TRUE);
        GlobalMembersGame_battle.GameBattle_GetSceneLand(battle_id,out start_pos,out scene_size);
        x = scene_size.w / 2 - 150;
        y = scene_size.h / 2;
        GameObject_SetX(player[0].object_, start_pos.x + x);
        GameObject_SetX(player[1].object_, start_pos.x + x + 300);
        GameObject_SetY(player[0].object_, start_pos.y + y);
        GameObject_SetY(player[1].object_, start_pos.y + y);
        GamePlayer_SetRightOriented(player[0]);
        GamePlayer_SetLeftOriented(player[1]);
        GamePlayer_SetStart(player[0]);
        GamePlayer_SetStart(player[1]);
    }

    public static int Game_InitFight(int role_id )
    {
        int ret, battle_id;
        LCUI_Graph_ stage_img;
        LCUI_Pos_ land_pos=new LCUI_Pos_();
        //ControlKey_ ctrlkey;
        LCUI_Size_ land_size, scene_size;

        Graph_Init(out stage_img);
        /* 载入战斗场景的图形资源 */
        GameGraphRes_GetGraph(DefineConstantsMain.SCENES_RES, "default", stage_img);

        battle_id = GlobalMembersGame_battle.GameBattle_New();

        scene_size = Graph_GetSize(stage_img);
        land_pos.x = 20;
        land_pos.y = 268;
        land_size.w = scene_size.w - 2 * land_pos.x;
        land_size.h = scene_size.h - land_pos.y;

        GameBattle_SetSceneBackground(battle_id, stage_img);
        GameBattle_SetSceneSize(battle_id, scene_size);
        GameBattle_SetSceneLand(battle_id, land_pos, land_size);

        /* 初始化游戏AI */
        GlobalMembersGame_AI.GameAI_Init();
        /* 初始化游戏数据帧处理 */
        GlobalMembersGame_battle.GameBattleFrame_Init(role_id, 20);
        /* 创建一个线程，用于处理游戏对象的数据更新 */
        GlobalMembersGame_battle.GameBattle_ProcGameObject(battle_id);
        return battle_id;
    }

    public int DemoGameBattle_Loop(int battle_id)
    {
        
        int i;
        int n;
        int n_found;
        BattleData_ p_battle;
        GamePlayer_ p_player;

        p_battle = GameBattle_GetBattle(battle_id);
        if (p_battle == null)
        {
            return -1;
        }
        //// 初始化游戏AI 
        ////GameAI_Init();
        //// 创建一个线程，用于处理游戏对象的数据更新 
        //GlobalMembersGame_battle.GameBattle_ProcGameObject(battle_id);
        //// 初始化游戏数据帧处理 
        //GameBattleFrame_Init(p_battle.data_proc_frame, 20);
        // 循环更新游戏数据 
        if (p_battle.data_proc_frame.state != BattleState.BATTLE_STATE_QUIT)
        {
            n = Queue_GetTotal(p_battle.player_list);
            for (n_found = 0, i = 0; i < n; ++i)
            {
                p_player = (GamePlayer_)Queue_Get(p_battle.player_list, i);
                if (p_player == null)
                {
                    continue;
                }
                if (!p_player.enable || !p_player.local_control)
                {
                    continue;
                }
                ++n_found;
                // 如果该游戏玩家不是由人类控制的 
                if (!p_player.human_control)
                {
                    if( LCUI_MSleep(1000))
                    GlobalMembersGame_AI.GameAI_Control(p_battle.id, p_player.id);
                }
                else
                {
                    GlobalMembersGame_control.GamePlayer_SyncKeyControl(p_player);
                }
                GlobalMembersGame_control.GamePlayer_SyncData(p_player);
                Widget_Update(p_player.object_);
                // 将第一个有效的游戏角色设置为镜头焦点 
                if (n_found == 1)
                {
                    p_battle.camera_target = p_player.object_;
                }
            }
            // 如果需要更新镜头 
            if (p_battle.need_sync_camera && p_battle.camera_target)
            {
                GameScene_UpdateCamera(p_battle.scene, p_battle.camera_target, p_battle.camera_x_padding);
            }
            // 处理攻击 
            Game_ProcAttack(ref p_battle.attack_record, p_battle.value_tip_proc);
            // 本帧数据处理完后，停留一段时间 
            GameBattleFrame_Remain(p_battle.data_proc_frame);
        }

        //Queue_Lock(battle_list);
        //n = Queue_GetTotal(battle_list);
        //for (i = 0; i < n; ++i)
        //{
        //    p_battle = (BattleData_)Queue_Get(battle_list, i);
        //    if (p_battle != null && p_battle.id == battle_id)
        //    {
        //        Queue_Delete(battle_list, i);
        //        break;
        //    }
        //}
        //Queue_Unlock(battle_list);
        return 0;
    }
}
